package com.book.controller;

import com.book.entity.BookType;
import com.book.service.BookTypeService;
import com.book.util.JsonResult;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author SuperYu  2024/7/30
 */

@RestController
@RequestMapping("/bookType")
@Slf4j
public class BookTypeController {
    @Resource
    BookTypeService bookTypeService;


    @GetMapping("/showByPage")
    @ResponseBody
    public JsonResult<PageInfo<BookType>> showPage(int pageNum, int pageSize) {
        PageInfo<BookType> pageInfo = bookTypeService.findBookTypeByPage(pageNum, pageSize);
        return JsonResult.ok(200, pageInfo);
    }

    @GetMapping("/showAll")
    @ResponseBody
    public JsonResult<List<BookType>> showPage() {
        List<BookType> list = bookTypeService.findAllBookType();
        return JsonResult.ok(200, list);
    }


    @PostMapping("/add")
    @ResponseBody
    public JsonResult<String> showPage(@Valid BookType bookType, BindingResult result) {
        log.info("要新增的账号：" + bookType);
        // 如果出错
        if (result.hasErrors()) {
            // 得到出错信息对象
            List<FieldError> listr = result.getFieldErrors();
            for (FieldError f : listr) {
                log.info("出错的属性是：" + f.getField() + " 提示信息是：" + f.getDefaultMessage());
            }

            return JsonResult.ok(500, "属性值有误！");
        } else {
            int i = bookTypeService.addBookType(bookType);
            if (i > 0) {
                return JsonResult.ok(200, "新增成功");

            }
            return JsonResult.ok(500, "新增失败");
        }
    }


    @GetMapping("/del/{id}")
    @ResponseBody
    public String del(@PathVariable int id) {
        System.out.println("要删除的账号id是：" + id);

        return "ok";
    }

    @GetMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String username) {
        HashMap<String, Boolean> map = new HashMap<>();

    /*    System.out.println("要检查的用户名是：" + username);

        BookType bookType = bookTypeService.findBookTypeByLogin(username);

        if (bookType == null) {

            map.put("valid", true);

        } else {

            map.put("valid", false);
        }*/
        return map;
    }

    @GetMapping("/checkNameByEdit")
    @ResponseBody
    public Map<String, Boolean> checkNameByEdit(int id, String username) {
        HashMap<String, Boolean> map = new HashMap<>();

        log.info("要检查的用户名是：" + username + "   id是：" + id);

        BookType bookType = bookTypeService.findBookTypeByEdit(id, username);

        if (bookType == null) {

            map.put("valid", true);

        } else {

            map.put("valid", false);
        }
        return map;
    }

}
